#include "CH395config.h"
ifconfig            if0_config;                         //socket0 配置 用于广播
ifconfig            if1_config;                         //socket1 配置 用于通信
uint8_t             CH395MACAddr[6];
extern uint8_t      Base_Station_id[3];
//--------------------------------------------------------
// CH395_tcp_client_param
// 初始化socket1 参数
//--------------------------------------------------------
void CH395_load_socket_param()
{
   //--socket1 参数加载
   memcpy(&if1_config, (uint8_t *)SOCKET1_FLASH_ADDRESS, sizeof(ifconfig));
   uint8_t* pif = &if1_config;

   //printf("if1config:");
   //for(int i=0; i<sizeof(ifconfig); i++)
   //{
   //   printf("0x%02x,",pif[i]);
   //}
   //printf("\r\n");
   if(if1_config.prototype==0xff)             //参数没有存放在flash中
   {
       if1_config.local_ip[0]     = 192;      //默认本地ip地址
       if1_config.local_ip[1]     = 168;
       if1_config.local_ip[2]     = 0;       
       if1_config.local_ip[3]     = 6;
           
       if1_config.local_getway[0] = 192;      //默认本地网关地址
       if1_config.local_getway[1] = 168;
       if1_config.local_getway[2] = 0;        
       if1_config.local_getway[3] = 1;
     
       if1_config.local_mask[0]   = 255;
       if1_config.local_mask[1]   = 255;
       if1_config.local_mask[2]   = 255;
       if1_config.local_mask[3]   = 0;
       
       if1_config.remote_ip[0]    =192;
       if1_config.remote_ip[1]    =168;
       if1_config.remote_ip[2]    =0;         
       if1_config.remote_ip[3]    =106;      

       if1_config.prototype       =PROTO_TYPE_TCP;
       if1_config.local_port      =8080;     
       if1_config.remote_port     =55082;      
   }

   //--socket0 参数加载
   memcpy(&if0_config, &if1_config, sizeof(ifconfig));
   memset(if0_config.remote_ip,255,4);         //设置目的ip为255.255.255.255,广播
   if0_config.local_port = 50000;
   if0_config.remote_port= 60000;
   if0_config.prototype  = PROTO_TYPE_UDP;
}

//--------------------------------------------------------
// CH395_ifconfig()
// 
//--------------------------------------------------------
void CH395_ifconfig()
{
  //printf("CH395 check exist input 0x57, output 0x%02x\r\n",ch395_check_exist(0x57));
  CH395_if_config(&if1_config);   //初始化ch395     
  ch395_set_keep_live_cnt(DEF_KEEP_LIVE_CNT);
  ch395_set_keep_live_idle(DEF_KEEP_LIVE_IDLE);
  ch395_set_keep_live_intvl(DEF_KEEP_LIVE_PERIOD);
}

//--------------------------------------------------------
// CH395_tcp_client_init
// socket1 配置成tcp client
//--------------------------------------------------------
void CH395_tcp_client_init()
{
  CH395_socket_init(1,&if1_config);
  CH395_socket_open(1);
  CH395_tcp_connect(1);
}

//--------------------------------------------------------
// CH395_udp_init
// socket0 配置成udp
//--------------------------------------------------------
void CH395_udp_init()
{
  CH395_socket_init(0,&if0_config);
  CH395_socket_open(0);
}


//--------------------------------------------------------
// udp_prot_parse()
// 
//--------------------------------------------------------
uint8_t  udp_prot_parse(uint8_t* buf)
{
   uint32_t len;
   pUDP_DATA net         = (pUDP_DATA)buf;                    
   pifconfig mCurrentCfg = &if1_config;
 
   if(memcmp(net->flag,NET_MODULE_FLAG,16)) 
     return 0;
   printf("UDP received:");
   for(uint8_t i=0; i<30;i++)
   {
      printf("%02x ",buf[i]);
   }
   printf("\r\n");

   switch  (net->cmd)
   {
     case  NET_MODULE_CMD_SEARCH :
           net->cmd  =  NET_MODULE_ACK_SEARCH ;
           memcpy(net->id,CH395MACAddr,6);
		       net->dat[0] = mCurrentCfg->local_ip[0];
		       net->dat[1] = mCurrentCfg->local_ip[1];
		       net->dat[2] = mCurrentCfg->local_ip[2];
		       net->dat[3] = mCurrentCfg->local_ip[3];
           net->dat[4] = 0x00;                        //填充
           net->dat[5] = Base_Station_id[0];
		       net->dat[6] = Base_Station_id[1];
		       net->dat[7] = Base_Station_id[2];
		       net->len = 8;
		       len = 32;
           UTIL_IdleMs(5);
           ch395_write_send_buf_sn(0, len, buf);
           UTIL_IdleMs(5);
           return 0;

     case   NET_MODULE_CMD_SET :
		   if(0!=memcmp(net->id,mCurrentCfg->local_ip,4))
				 return 0;
  
       CH395_socket_close(0);
       net->cmd  = NET_MODULE_ACK_SET ;
       net->len  = MODULE_CFG_LEN+4;   
					 
       memcpy(&if1_config,net->dat+4,MODULE_CFG_LEN);
       memcpy(&if0_config,net->dat+4,MODULE_CFG_LEN);
       memset(if0_config.remote_ip,255,4);         //设置目的ip为255.255.255.255,广播
       if0_config.local_port = 50000;
       if0_config.remote_port= 60000;
       if0_config.prototype  = PROTO_TYPE_UDP;
       if1_config.prototype = PROTO_TYPE_TCP;

       len = MODULE_CFG_LEN+28;
       UTIL_IdleMs(5);
       CH395_udp_init();
       ch395_write_send_buf_sn(0, len, buf);
       UTIL_IdleMs(5);

       FLASH_Unlock();
       FLASH_Erase(SOCKET1_FLASH_ADDRESS, sizeof(ifconfig));
       FLASH_FastProgram(SOCKET1_FLASH_ADDRESS, &if1_config, sizeof(if1_config));
       FLASH_Lock();
       UTIL_IdleMs(30);
       return 1;

    case   NET_MODULE_CMD_GET :
		   if(0!=memcmp(net->id,mCurrentCfg->local_ip,4))
			   return 0;
       net->cmd  = NET_MODULE_ACK_GET ;
       net->len = MODULE_CFG_LEN+4; 	 
       net->dat[0] = 0x00;
		   memcpy(net->dat+1,Base_Station_id,3);              //Base_Station_id是3个字节数据
       memcpy(net->dat+4,&if1_config,MODULE_CFG_LEN)  ;
       len = MODULE_CFG_LEN+28;
       UTIL_IdleMs(5);
       ch395_write_send_buf_sn(0, len, buf); 
       UTIL_IdleMs(5);
       return 0;
         
    case   NET_MODULE_CMD_RESET :
       if(0!=memcmp(net->id,CH395MACAddr,6)) 
         return 0;
       CH395_socket_close(0);      
       FLASH_Unlock();
       FLASH_Erase(SOCKET1_FLASH_ADDRESS, 3);
       FLASH_Lock();
       UTIL_IdleMs(30);
       return 1;
    case  NET_MODULE_CMD_RESTART:
    	 if(0!=memcmp(net->id,Base_Station_id,3))
			   return 0;
       SYS_SoftwareReset();
       UTIL_IdleMs(30);
       return 1;
    default :
       return 0 ;
   }

}